package com.smashit.tacocloud.service;

import com.smashit.tacocloud.domain.Ingredient;
import org.h2.jdbcx.JdbcDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author jinshan_jiang
 * @version 1.0
 * @since 2024/4/9
 */
@Service
public class IngredientService {
    // @Autowired
    // private JdbcDataSource dataSource;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Ingredient findOne(String id) {
        JdbcDataSource dataSource = new JdbcDataSource();
        ResultSet resultSet = null;
        try(Connection connection = dataSource.getConnection();
            PreparedStatement statement = connection.prepareStatement("select id, name, type from ingredient where id = ?");
        ) {
            statement.setString(1, id);
            resultSet = statement.executeQuery();
            Ingredient ingredient = null;
            if (resultSet.next()) {
                ingredient = new Ingredient(resultSet.getString("id"), resultSet.getString("name"), Ingredient.Type.valueOf(resultSet.getString("type")));
            }
            resultSet.close();
            return ingredient;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        /*Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement("select id, name, type from ingredient where id = ?");
            statement.setString(1, id);
            resultSet = statement.executeQuery();
            Ingredient ingredient = null;
            if (resultSet.next()) {
                ingredient = new Ingredient(resultSet.getString("id"), resultSet.getString("name"), Ingredient.Type.valueOf(resultSet.getString("type")));
            }
            return ingredient;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }*/

        return null;
    }

    public Ingredient findOneByJdbcTemplate(String id) {
        return jdbcTemplate.queryForObject("select id,name,type from ingredient where id = ?", this::mapRowToIngredient, id);
    }

    private Ingredient mapRowToIngredient(ResultSet resultSet, int rowNum) throws SQLException {
        return new Ingredient(resultSet.getString("id"), resultSet.getString("name"), Ingredient.Type.valueOf(resultSet.getString("type")));
    }
}
